﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoVanHocTapData.Infrastructure;
using CoVanHocTapData.Entities;
using CoVanHocTapData.Interface;

namespace CoVanHocTapData.Concretes
{
    public class SinhVienRepository : ISinhVienRepository
    {
        DangKyPhuEntities entities;


        public SinhVienRepository()
        {
            entities = new DangKyPhuEntities();
        }

        #region Repository

        #endregion

        public Entities.SinhVien LaySVDangNhap()
        {
            throw new NotImplementedException();
        }

        public SinhVien LaySVDangNhap(string strMSSV)
        {
            return (from m in entities.SinhViens
                    where m.MSSV.Equals(strMSSV)
                    select m).ToList().FirstOrDefault();
        }

        public SinhVien LaySVTuMaSV(string maSV)
        {
            return (from m in entities.SinhViens
                    where m.MSSV == maSV
                    select m).FirstOrDefault();
        }

        public float DiemTrungBinhTichLuyCua(string maSV)
        {

            throw new NotImplementedException();
        }

        public float? TinChiTichLuyCua(string maSV)
        {
            KetQuaHocRepository ketQuaRepo = new KetQuaHocRepository();
            List<MonHoc> dsmhDaHT = new List<MonHoc>();
            List<MonHoc> dsmhChuaHT = new List<MonHoc>();



            ketQuaRepo.DSMonHocTichLuy(maSV, 0, out dsmhDaHT, out dsmhChuaHT, out dsmhChuaHT, out dsmhChuaHT);
            float? tongTC = 0;

            foreach (var kqh in dsmhDaHT)
            {
                tongTC += kqh.SoDVHocTap;
            }
            return tongTC;
        }

        public void TichLuyBatBuocCua(string maSV, out string dsMHBatBuocDaHT, out float? tcTichLuyBatBuoc, out string dsMHBatBuocChuaHT)
        {
            tcTichLuyBatBuoc = 0;
            dsMHBatBuocDaHT = "";
            dsMHBatBuocChuaHT = "";
            KetQuaHocRepository ketQuaRepository = new KetQuaHocRepository();
            MonHocRepository monHocRepo = new MonHocRepository();
            List<MonHoc> dsmhDaHT = new List<MonHoc>();
            List<MonHoc> dsmhChuaHT = new List<MonHoc>();
            List<MonHoc> _tnull = new List<MonHoc>();
            ketQuaRepository.DSMonHocTichLuy(maSV, 1, out dsmhDaHT, out dsmhChuaHT, out _tnull, out _tnull);

            foreach (var kqh in dsmhDaHT)
            {
                tcTichLuyBatBuoc += kqh.SoDVHocTap;

            }
            foreach (var kqh in dsmhChuaHT)
            {
                //tcTichLuyBatBuoc += kqh.SoDVHocTap;                
                dsMHBatBuocChuaHT += kqh.MonHocID + " - " + kqh.TenMonHoc + "</br>";
            }
        }

        public float? TinChiTichLuyTuChonCua(string maSV)
        {
            KetQuaHocRepository ketQuaRepository = new KetQuaHocRepository();
            List<MonHoc> dsmhDaHT = new List<MonHoc>();
            List<MonHoc> _tnull = new List<MonHoc>();
            ketQuaRepository.DSMonHocTichLuy(maSV, 2, out dsmhDaHT, out _tnull, out _tnull, out _tnull);
            float? tongTC = 0;
            
                tongTC = 0;
                foreach (var kqh in dsmhDaHT)
                {
                    //if (tongTC <= soTCNhom.soTC)
                        tongTC += kqh.SoDVHocTap;
                }
                return tongTC;
           
        }

        public void TichLuyTuChonCua(string maSV, out List<KhoiCTDT_TC> soTCTichLuyTheoNhom, out float soTCTichLuy_Thuc)
        {
            soTCTichLuyTheoNhom = new List<KhoiCTDT_TC>();
            soTCTichLuy_Thuc = 0;
            KhoiCTDT_TC khoiCTDT_tC = null;

            NganhDaoTao nganhDaoTao = new NganhDaoTao();
            HeDaoTao heDaoTao = new HeDaoTao();
            NienKhoa nienKhoa = new NienKhoa();
            KhoiLopRepository khoiLopRepo = new KhoiLopRepository();
            KhoiChuongTrinhDaoTaoRepository khoiCTDTRepo = new KhoiChuongTrinhDaoTaoRepository();
            khoiLopRepo.KhoiLop(maSV, out nganhDaoTao, out heDaoTao, out nienKhoa); // gán khối lớp

            KetQuaHocRepository ketQuaRepository = new KetQuaHocRepository();
            List<MonHoc> dsmhDaHT = new List<MonHoc>();
            List<MonHoc> _tnull = new List<MonHoc>();
            ketQuaRepository.DSMonHocTichLuy(maSV, 2, out dsmhDaHT, out _tnull, out _tnull, out _tnull);

            var dsKhoiNhomCTDT = khoiCTDTRepo.DSKhoiNhomCTDT(maSV);
            foreach (var khoiNhom in dsKhoiNhomCTDT)
            {
                khoiCTDT_tC = new KhoiCTDT_TC();
                khoiCTDT_tC.soTC = 0;
                khoiCTDT_tC.dsmhDaHT = new List<MonHoc>();
                float _tempTC = 0;
                foreach (var kqh in dsmhDaHT)
                {
                    if (khoiCTDTRepo.LayKhoiCTDTTuMaMonHoc(kqh.MonHocID, nganhDaoTao.NganhID).ID == khoiNhom.ID)
                    {
                        khoiCTDT_tC.soTC += kqh.SoDVHocTap.Value;
                        khoiCTDT_tC.dsmhDaHT.Add(kqh);
                        khoiCTDT_tC.khoiCTDT = khoiCTDTRepo.LayKhoiCTDTTuMaMonHoc(kqh.MonHocID, nganhDaoTao.NganhID);
                        if (khoiCTDT_tC.soTC >= khoiCTDT_tC.khoiCTDT.SoTCBatBuoc)
                            _tempTC = khoiCTDT_tC.khoiCTDT.SoTCBatBuoc.Value;
                        else
                            _tempTC = khoiCTDT_tC.soTC;
                    }
                                    
                }
                soTCTichLuy_Thuc += _tempTC;
                soTCTichLuyTheoNhom.Add(khoiCTDT_tC);
            }
            // Nghiên cứu thêm List<List<T>> ~> tạm gọi là List 2 chiều
        }

        public int DiemToeicDaHT(string maSV)
        {
            KetQuaHocRepository ketQuaRepository = new KetQuaHocRepository();
            var monAVGTCaoNhatDaHT = ketQuaRepository.LayMonAVGTCaoNhatDaHT(maSV);
            int maMonInt = Int32.Parse(monAVGTCaoNhatDaHT.MonHocID);
            switch (monAVGTCaoNhatDaHT.MonHocID)
            {
                case "001001": return 250;

                case "001002": return 300;

                case "001003": return 350;

                case "001004": return 400;

                case "001005": return 450;

                case "001006": return 500;

                default: return 0;
            }
        }

        #region Danh sách môn học
        public string DSMonAVGTDaHT(string maSV)
        {
            //KetQuaHocRepository ketQuaRepository = new KetQuaHocRepository();
            MonHocRepository monHocRepo = new MonHocRepository();
            var dsMonAVGTDaHT = monHocRepo.LayDanhSachAVGTDaHT(maSV);

            string dsmon = "";
            foreach (var mon in dsMonAVGTDaHT)
            {
                dsmon += mon.MonHocID + " - " + mon.TenMonHoc + "</br>";
            }
            return dsmon;
        }

        public string DSMonAVGTChuaHT(string maSV)
        {
            throw new NotImplementedException();
        }

        public string DSMonGDTCDaHT(string maSV)
        {
            MonHocRepository monHocRepo = new MonHocRepository();
            var dsMonDaHT = monHocRepo.LayDanhSachGDTCDaHT(maSV);
            string dsmon = "";
            foreach (var mon in dsMonDaHT)
            {
                dsmon += mon.MonHocID + " - " + mon.TenMonHoc + "</br>";
            }
            return dsmon;
        }

        public string DSMonGDTCChuaHT(string maSV)
        {
            throw new NotImplementedException();
        }

        public string DSMonGDQPDaHT(string maSV)
        {
            MonHocRepository monHocRepo = new MonHocRepository();
            var dsMonDaHT = monHocRepo.LayDanhSachGDQPDaHT(maSV);
            string dsmon = "";
            foreach (var mon in dsMonDaHT)
            {
                dsmon += mon.MonHocID + " - " + mon.TenMonHoc + "</br>";
            }
            return dsmon;
        }

        public string DSMonGDQPChuaHT(string maSV)
        {
            throw new NotImplementedException();
        }
        #endregion


        public float DiemTrungBinhTichLuyCN2Cua(string maSV)
        {

            throw new NotImplementedException();
        }

        public float? TinChiTichLuyCN2Cua(string maSV)
        {
            KetQuaHocRepository ketQuaRepo = new KetQuaHocRepository();
            List<MonHoc> dsmhDaHT = new List<MonHoc>();
            List<MonHoc> dsmhChuaHT = new List<MonHoc>();
            ketQuaRepo.DSMonHocTichLuyCN2(maSV, 0, out dsmhDaHT, out dsmhChuaHT);
            float? tongTC = 0;

            foreach (var kqh in dsmhDaHT)
            {
                tongTC += kqh.SoDVHocTap;
            }
            return tongTC.Value;
        }

        public void TichLuyBatBuocCN2Cua(string maSV, out string dsMHBatBuocDaHT, out float? tcTichLuyBatBuoc, out string dsMHBatBuocChuaHT)
        {
            tcTichLuyBatBuoc = 0;
            dsMHBatBuocDaHT = "";
            dsMHBatBuocChuaHT = "";
            KetQuaHocRepository ketQuaRepository = new KetQuaHocRepository();
            MonHocRepository monHocRepo = new MonHocRepository();
            List<MonHoc> dsmhDaHT = new List<MonHoc>();
            List<MonHoc> dsmhChuaHT = new List<MonHoc>();
            ketQuaRepository.DSMonHocTichLuyCN2(maSV, 1, out dsmhDaHT, out dsmhChuaHT);

            foreach (var kqh in dsmhDaHT)
            {
                tcTichLuyBatBuoc += kqh.SoDVHocTap;
            }
            foreach (var kqh in dsmhChuaHT)
            {           
                dsMHBatBuocChuaHT += kqh.MonHocID + " - " + kqh.TenMonHoc + "</br>";
            }
        }

        public float? TinChiTichLuyTuChonCN2Cua(string maSV)
        {
            KetQuaHocRepository ketQuaRepository = new KetQuaHocRepository();
            List<MonHoc> dsmhDaHT = new List<MonHoc>();
            List<MonHoc> dsmhChuaHT = new List<MonHoc>();
            ketQuaRepository.DSMonHocTichLuyCN2(maSV, 2, out dsmhDaHT, out dsmhChuaHT);
            float? tongTC = 0;

            tongTC = 0;
            foreach (var kqh in dsmhDaHT)
            {
                //if (tongTC <= soTCNhom.soTC)
                tongTC += kqh.SoDVHocTap;
            }
            return tongTC;
        }

        public void TichLuyTuChonCN2Cua(string maSV, out List<KhoiCTDT_TC> soTCTichLuyTheoNhom, out float soTCTichLuy_Thuc)
        {
            soTCTichLuyTheoNhom = new List<KhoiCTDT_TC>();
            soTCTichLuy_Thuc = 0;
            KhoiCTDT_TC khoiCTDT_tC = null;

            NganhDaoTao nganhDaoTao = new NganhDaoTao();
            HeDaoTao heDaoTao = new HeDaoTao();
            NienKhoa nienKhoa = new NienKhoa();
            KhoiLopRepository khoiLopRepo = new KhoiLopRepository();
            KhoiChuongTrinhDaoTaoRepository khoiCTDTRepo = new KhoiChuongTrinhDaoTaoRepository();
            khoiLopRepo.KhoiLopCN2(maSV, out nganhDaoTao, out heDaoTao, out nienKhoa); // gán khối lớp

            // tính số lượng khối lớp
            
            KetQuaHocRepository ketQuaRepository = new KetQuaHocRepository();
            List<MonHoc> dsmhDaHT = new List<MonHoc>();
            List<MonHoc> dsmhChuaHT = new List<MonHoc>();
            ketQuaRepository.DSMonHocTichLuyCN2(maSV, 2, out dsmhDaHT, out dsmhChuaHT);
            
            var dsKhoiNhomCTDT = khoiCTDTRepo.DSKhoiNhomCTDT_CN2(maSV);
            foreach(var khoiNhom in dsKhoiNhomCTDT)
            {
                khoiCTDT_tC = new KhoiCTDT_TC();
                khoiCTDT_tC.soTC = 0;
                khoiCTDT_tC.dsmhDaHT = new List<MonHoc>();
                float _tempTC = 0;
                foreach (var kqh in dsmhDaHT)
                {                   
                    if (khoiCTDTRepo.LayKhoiCTDTTuMaMonHoc(kqh.MonHocID, nganhDaoTao.NganhID).ID == khoiNhom.ID)
                    {
                        khoiCTDT_tC.soTC += kqh.SoDVHocTap.Value;
                        khoiCTDT_tC.dsmhDaHT.Add(kqh);
                        khoiCTDT_tC.khoiCTDT = khoiCTDTRepo.LayKhoiCTDTTuMaMonHoc(kqh.MonHocID, nganhDaoTao.NganhID);
                        if (khoiCTDT_tC.soTC >= khoiCTDT_tC.khoiCTDT.SoTCBatBuoc)
                            _tempTC = khoiCTDT_tC.khoiCTDT.SoTCBatBuoc.Value;
                        else
                            _tempTC = khoiCTDT_tC.soTC;
                    }

                }
                soTCTichLuy_Thuc += _tempTC;
                soTCTichLuyTheoNhom.Add(khoiCTDT_tC);
            }
        }

        #region ISinhVienRepository Members


        public void UpdateMatKhauChoSV(SinhVien sv, string mkMoi)
        {
            var result = (from m in entities.SinhViens
                         where m.MSSV.Equals(sv.MSSV)
                         select m).FirstOrDefault();
            result.MatKhau = mkMoi;
            entities.SaveChanges();

        }

        #endregion


        public string DSMonBatBuocChuaHT(string maSV)
        {
            throw new NotImplementedException();
        }






        public void DSMonHocTruocTienQuyet(string chuoiMaMH, out List<MonHoc> dsMHTruoc, out List<MonHoc> dsMHTienQuyet)
        {
            throw new NotImplementedException();
        }

        public List<SinhVien> LayDsSVTheoKhoaHoc(string nganhID, string heDaoTaoID, int nienKhoaID)
        {
            var result = from m in entities.SinhViens
                         where m.Lop.NganhID.Equals(nganhID)
                         && m.Lop.HeDaoTaoID.Equals(heDaoTaoID)
                         && m.Lop.NienKhoaID.Value == nienKhoaID
                         select m;
            return result.ToList();
        }
    }
}
